package com.imooc.distributedemo.resume.leetcode;

/**
 * leetcode50 pow(x,n)  获取整数x的n次幂
 */
public class Solution50 {
    public static void main(String[] args) {
        double x=3.000000;
        int n=4;
        double result = myPow(x,n);
        System.out.println(result);
    }

    public static double myPow(double x, int n) {
        long N = n;
        return N >= 0 ? quickMul(x, N) : 1.0 / quickMul(x, -N);
    }

    public static double quickMul(double x, long N) {
        if (N == 0) {
            return 1.0;
        }
        double y = quickMul(x, N / 2);
        return N % 2 == 0 ? y * y : y * y * x;
    }


    /*public static double myPow(double x, int n) {
        if(x==0 && n<=0){
            return 0;
        }
        if(x == 0 ){
            return 1;
        }

        if(n == 1){
            return x;
        }

        int num = n-1;
        double result = x;
        while(num>0){
            result *= x;
            num--;
        }
        if(n<0){
            result = 1/result;
        }
        return result;
    }*/
}
